package com.woniuxy.springbootrabbit.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class KillConfiguration {
    /**
     * 配置正常的消息队列、交换机
     */
    @Bean
    public Queue killQueue(){
        //指定消息队列的参数
        Map<String,Object> map = new HashMap<>();

        //指定死信交换机
        map.put("x-dead-letter-exchange","kill_dead_exchange");

        //指定死信路由
        map.put("x-dead-letter-routing-key","kill_dead");

        //指定消息队列的长度
        map.put("x-max-length",100);

        //指定消息溢出时处理方式
        //drop-head：删除队列头，删除溢出的消息变为死信
        //reject-publish：拒绝接收新的消息
        map.put("x-overflow","drop-head");

        //
        return new Queue("kill_queue",false,false,false,map);
    }
    @Bean
    public DirectExchange killExchange(){
        return new DirectExchange("kill_exchange");
    }
    @Bean
    public Binding bindingKillQueueToKillExchange(Queue killQueue, DirectExchange killExchange){
        return BindingBuilder.bind(killQueue).to(killExchange).with("kill");
    }

    /**
     * 死信消息队列、交换机
     */
    @Bean
    public Queue killDeadQueue(){
        return new Queue("kill_dead_queue");
    }
    @Bean
    public DirectExchange killDeadExchange(){
        return new DirectExchange("kill_dead_exchange");
    }
    @Bean
    public Binding bindingKillDeadQueueToKillDeadExchange(Queue killDeadQueue, DirectExchange killDeadExchange){
        return BindingBuilder.bind(killDeadQueue).to(killDeadExchange).with("kill_dead");
    }
}
